package ui.panel
{

	import com.greensock.plugins.VolumePlugin;

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.text.TextField;

	import globals.ButtonType;
	import globals.Global;
	import globals.Setting;

	import ui.component.Button;
	import ui.component.GameTextField;

	import util.DisplayUtil;
	import util.TextStyle;
	import util.UIUtil;

	public class BasePanel extends Sprite
	{
		public var pid:uint;
		public var opened:Boolean = false;
		public var isMask:Boolean = false;
		/////////////////////////////
		protected var _title:TextField;
		protected var _titleBg:Bitmap;
		protected var _bg:Bitmap = new Bitmap();
		protected var _closeButton:Button;

		public function BasePanel()
		{
			super();
			initUI();
		}

		protected function initUI():void
		{
			this.addChild(_bg);
			updateBg();

			_closeButton = UIUtil.getButton(0, ButtonType.BUTTON_PANEL_CLOSE, closeHandler);
			this.addChildAt(_closeButton, 0);
			_closeButton.x = _bg.x + _bg.width - 24;
			_closeButton.y = _bg.y + 25;

			_titleBg = new Bitmap();
			_titleBg.bitmapData = Global.assetsManager.getAssetsAsBitmapData("title_bg");
			_titleBg.x = _bg.x + (_bg.width - _titleBg.width) * 0.5;
			_titleBg.y = _bg.y - 8;
			this.addChild(_titleBg);

			_title = new GameTextField();
			_title.defaultTextFormat = TextStyle.getTextFormat(0x653011, 20, true, "center");
			_title.x = _titleBg.x;
			_title.y = _bg.y;
			TextStyle.addTextFrame(_title);
			_title.width = _titleBg.width;
			this.addChild(_title);
		}

		private function closeHandler(button:Button):void
		{
			close();
		}

		public function updateTitle(titleName:String):void
		{
			_title.text = titleName;
		}

		protected function updateBg():void
		{
			_bg.bitmapData = Global.assetsManager.getAssetsAsBitmapData("panel_bg");
		}

		private var bgMask:BackgroundMask;

		public function open(center:Boolean = true):void
		{
			if(center)
			{
				this.x = int((Setting.STAGE_WIDTH - _bg.width) * 0.5);
				this.y = int((Setting.STAGE_HEIGHT - _bg.height) * 0.5);
			}

			if(isMask)
			{
				if(bgMask == null)
				{
					bgMask = new BackgroundMask();
				}
				DisplayUtil.addChild(bgMask, Global.game.panelLayer);
			}
			Global.game.panelLayer.addChild(this);
			opened = true;
		}

		public function close():void
		{
			if(isMask)
			{
				DisplayUtil.removeChild(Global.game.panelLayer, bgMask);
			}
			DisplayUtil.removeChild(this.parent, this);
			opened = false;
			clear();
		}

		public function clear():void
		{

		}

		public function dispose():void
		{
			_bg.bitmapData = null;
		}
	}
}
import flash.display.Shape;

import globals.Setting;

/**蒙板**/
class BackgroundMask extends Shape
{
	public function BackgroundMask()
	{
		with(this.graphics)
		{
			beginFill(0x000000, 0.3);
			drawRect(0, 0, Setting.STAGE_WIDTH, Setting.STAGE_HEIGHT);
			endFill();
		}
	}
}